package com.yc.cloudvideo.sp_store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yc.cloudvideo.biz.BizException;
import com.yc.cloudvideo.entity.File;
import com.yc.cloudvideo.entity.StoreFile;
import com.yc.cloudvideo.sp_store.mapper.FileMapper;
import com.yc.cloudvideo.sp_store.mapper.StoreFileMapper;
import com.yc.cloudvideo.sp_store.service.IFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.common.bean.Result;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LYYT
 * @since 2022-08-03
 */
@ConfigurationProperties(value = "myconfig.upload",
        ignoreInvalidFields = true)
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements IFileService {

    private String uploadDir;
    private String webpath;

    //文件上传，判断文件是否存在，保存数据库记录，file，fileStore表，保存文件
    @Resource
    FileMapper fileMapper;
    @Resource
    StoreFileMapper storeFileMapper;
    @Override
    @Transactional(rollbackFor = {BizException.class,IOException.class})
    public Result upload(int uid,int cid, String savePath, MultipartFile file) throws com.yc.common.biz.BizException, IOException {
        String filename = file.getOriginalFilename();
        System.out.println("=========init upload=====================");
        System.out.println(savePath);
        System.out.println("=========init upload=====================");

        //将左斜杠替换成右斜杠
        savePath=savePath.replaceAll("/","\\\\");//英文
        //与文件名拼接诶
        savePath+="\\"+filename;

        //查询数据库中是否存在该文件
        QueryWrapper<File> wrapper=new QueryWrapper();
        wrapper.eq("path",savePath);
        Long count = fileMapper.selectCount(wrapper);
        if(count!=0){
            //存在，返回
            throw new com.yc.common.biz.BizException("文件已存在,请更换名字!");
        }

        //不存在，向file表添加一条数据
        File file1=new File(filename, LocalDateTime.now(), (long) uid,savePath);
        fileMapper.insert(file1);

        //向store_file添加一条数据
        StoreFile storeFile=new StoreFile(LocalDateTime.now(), (long) cid,file1.getFid(), 0L);
        storeFileMapper.insert(storeFile);

        //保存文件 // 1 1  upload\1\2\3\s.mp4
        //将web路径替换为磁盘路径

        savePath=savePath.replace(webpath,uploadDir);

        //保存文件
        file.transferTo(new java.io.File(savePath));

        //构建web路径
        String fileWebPath=savePath.replace(uploadDir,webpath);
        //返回
        return new Result(1,"文件上传成功", fileWebPath);

    }


    public String getUploadDir() {
        return uploadDir;
    }

    public void setUploadDir(String uploadDir) {
        this.uploadDir = uploadDir;
    }

    public String getWebpath() {
        return webpath;
    }

    public void setWebpath(String webpath) {
        this.webpath = webpath;
    }
}
